
#region Imported Namespaces
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.DirectoryServices;
using System.Drawing;
using System.Windows.Forms;

using Authenticator = PHSRAG.Security.Authenticator;
using CenterDialog = PHSRAG.Utility.CenterDialog;
using Crypt = PHSRAG.Utility.Crypt;
#endregion

namespace SecurityTest
{
	/// <summary>
	/// The SecurityTest class is used to test features offered by the Security assembly.
	/// </summary>
	public class SecurityTest : Form
	{
		#region Instance Variables
		private Container		components = null;
		private ErrorProvider	errorProvider;
		private TabControl		tab;

		#region GetInfo Test Variables
		private Button			getInfo;
		private TabPage			getInfoPage;
		private ListView		info;
		private ColumnHeader	nameColumn;
		private TextBox			userName;
		private Label			userNameLabel;
		private ColumnHeader	valueColumn;
		#endregion

		#region Authenticator Test Variables
		private Button			authenticate;
		private Label			authenticationLabel;
		private TabPage			authenticatorTestPage;
		private Button			impersonate;
		private TextBox			login;
		private Label			loginLabel;
		private ComboBox		loginType;
		private Label			loginTypeLabel;
		private Label			logonProviderLabel;
		private ComboBox		logonProvider;
		private TextBox			password;
		private Label			passwordLabel;
		private TextBox			status;
		#endregion

		#region Crypt Test Variables
		private TabPage			cryptPage;
		private Button			decrypt;
		private Label			decryptedTextLabel;
		private Button			encrypt;
		private TextBox			encryptedText;
		private TextBox			plainText;
		private Label			plainTextLabel;
		#endregion
		#endregion

		#region Constructor
		public SecurityTest()
		{
			InitializeComponent();

			foreach (string key in Enum.GetNames(typeof(Authenticator.LogonType)))
				loginType.Items.Add(key);
			loginType.SelectedIndex = 0;

			foreach (string key in Enum.GetNames(typeof(Authenticator.LogonProvider)))
				logonProvider.Items.Add(key);
			logonProvider.SelectedIndex = 0;
		}
		#endregion

		#region Overrides
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
				components.Dispose();
			base.Dispose(disposing);
		}
		#endregion

		#region Event Handlers
		#region Authenticator Test Event Handlers
		private void OnAuthenticate(object sender, System.EventArgs e)
		{
			status.Text = "";

			string userNameEntry, passwordEntry;
			if (ValidateAuthenticationEntries(out userNameEntry, out passwordEntry))
			{
				Cursor saveCursor = Cursor;
				Cursor = Cursors.WaitCursor;
				Application.DoEvents();

				try
				{
					Authenticator.ValidateCredentials(userNameEntry, passwordEntry);
					status.Text = "Credentials are valid";
				}
				catch (Exception ex)
				{
					status.Text = ex.Message;
				}
				finally
				{
					Cursor = saveCursor;
				}
			}
		}

		private void OnImpersonate(object sender, System.EventArgs e)
		{
			status.Text = "";

			string userNameEntry, passwordEntry;
			if (ValidateAuthenticationEntries(out userNameEntry, out passwordEntry))
			{
				Cursor saveCursor = Cursor;
				Cursor = Cursors.WaitCursor;
				Application.DoEvents();

				try
				{
					PHSRAG.Security.Authenticator.ImpersonateUser(userNameEntry, passwordEntry,
						(Authenticator.LogonType)Enum.Parse(typeof(Authenticator.LogonType), (string)loginType.SelectedItem),
						(Authenticator.LogonProvider)Enum.Parse(typeof(Authenticator.LogonProvider), (string)logonProvider.SelectedItem));
					status.Text = "Impersonation successful";
				}
				catch (Exception ex)
				{
					status.Text = ex.Message;
				}
				finally
				{
					Cursor = saveCursor;
				}
			}
		}
		#endregion

		#region Get Info event handlers
		private void OnGetInfo(object sender, System.EventArgs e)
		{
			string loginEntry;
			if (ValidateGetInfoEntries(out loginEntry))
			{
				UserInfo userInfo = new UserInfo();
				if (userInfo.ShowDialog(this) == DialogResult.OK)
				{
					string[] infoToGet = userInfo.GetAll ? null : userInfo.InfoToGet.Split(',');

					Cursor saveCursor = Cursor;
					Cursor = Cursors.WaitCursor;
					info.Items.Clear();

					try
					{
						SearchResult result = Authenticator.GetAttributes(loginEntry, infoToGet);
						if (result != null)
						{
							ResultPropertyCollection properties = result.Properties;
							foreach (string name in properties.PropertyNames)
							{
								if (!(properties[name][0] is Byte[]))
									info.Items.Add(new ListViewItem(new string[]{name, (properties[name][0]).ToString()}));
							}
						}
					}
					catch (Exception ex)
					{
						CenterDialog dialog = new CenterDialog(this);
						dialog.BeginCentering();
						MessageBox.Show(this, ex.Message, "Exception Caught", MessageBoxButtons.OK, MessageBoxIcon.Error);
						dialog.EndCentering();
					}
					finally
					{
						Cursor = saveCursor;
					}
				}
			}
		}
		#endregion

		#region Crypt Test Event Handlers
		private void OnDecrypt(object sender, System.EventArgs e)
		{
			try
			{
				string inputString = encryptedText.Text.Trim();
				if (inputString.Length == 0)
					throw new Exception("You must specify encrypted text");
				plainText.Text = Crypt.DecryptFromString(inputString);
				errorProvider.SetError(encryptedText, "");
			}
			catch (Exception ex)
			{
				errorProvider.SetError(encryptedText, ex.Message);
			}
		}

		private void OnEcrypt(object sender, System.EventArgs e)
		{
			try
			{
				string inputString = plainText.Text.Trim();
				if (inputString.Length == 0)
					throw new Exception("You must specify plain text");
				encryptedText.Text = Crypt.EncryptToString(inputString);
				errorProvider.SetError(plainText, "");
			}
			catch (Exception ex)
			{
				errorProvider.SetError(plainText, ex.Message);
			}
		}
		#endregion
		#endregion

		#region Private Methods
		#region Authenticator Test Methods
		private bool ValidateAuthenticationEntries(out string userNameEntry, out string passwordEntry)
		{
			bool result = false;
			Control currentField = null;
			userNameEntry = userName.Text.Trim();
			passwordEntry = password.Text.Trim();

			try
			{
				currentField = userName;
				if (userNameEntry.Length == 0)
					throw new Exception("You must specifiy a user name");
				errorProvider.SetError(userName, "");

				currentField = password;
				if (passwordEntry.Length == 0)
					throw new Exception("You must specifiy a password");
				errorProvider.SetError(password, "");
				result = true;
			}
			catch (Exception ex)
			{
				errorProvider.SetError(currentField, ex.Message);
			}
			return result;
		}
		#endregion

		#region Get Info methods
		private bool ValidateGetInfoEntries(out string loginEntry)
		{
			bool result = false;
			Control currentField = null;
			loginEntry = login.Text.Trim();
			try
			{
				currentField = login;
				if (loginEntry.Length == 0)
					throw new Exception("You must specify a login name");
				errorProvider.SetError(login, "");
				result = true;
			}
			catch (Exception ex)
			{
				errorProvider.SetError(currentField, ex.Message);
			}
			return result;
		}
		#endregion
		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tab = new System.Windows.Forms.TabControl();
			this.getInfoPage = new System.Windows.Forms.TabPage();
			this.info = new System.Windows.Forms.ListView();
			this.nameColumn = new System.Windows.Forms.ColumnHeader();
			this.valueColumn = new System.Windows.Forms.ColumnHeader();
			this.getInfo = new System.Windows.Forms.Button();
			this.login = new System.Windows.Forms.TextBox();
			this.loginLabel = new System.Windows.Forms.Label();
			this.authenticatorTestPage = new System.Windows.Forms.TabPage();
			this.loginTypeLabel = new System.Windows.Forms.Label();
			this.loginType = new System.Windows.Forms.ComboBox();
			this.status = new System.Windows.Forms.TextBox();
			this.authenticate = new System.Windows.Forms.Button();
			this.userName = new System.Windows.Forms.TextBox();
			this.passwordLabel = new System.Windows.Forms.Label();
			this.userNameLabel = new System.Windows.Forms.Label();
			this.authenticationLabel = new System.Windows.Forms.Label();
			this.password = new System.Windows.Forms.TextBox();
			this.impersonate = new System.Windows.Forms.Button();
			this.cryptPage = new System.Windows.Forms.TabPage();
			this.decrypt = new System.Windows.Forms.Button();
			this.decryptedTextLabel = new System.Windows.Forms.Label();
			this.encrypt = new System.Windows.Forms.Button();
			this.plainText = new System.Windows.Forms.TextBox();
			this.plainTextLabel = new System.Windows.Forms.Label();
			this.encryptedText = new System.Windows.Forms.TextBox();
			this.errorProvider = new System.Windows.Forms.ErrorProvider();
			this.logonProviderLabel = new System.Windows.Forms.Label();
			this.logonProvider = new System.Windows.Forms.ComboBox();
			this.tab.SuspendLayout();
			this.getInfoPage.SuspendLayout();
			this.authenticatorTestPage.SuspendLayout();
			this.cryptPage.SuspendLayout();
			this.SuspendLayout();
			// 
			// tab
			// 
			this.tab.Controls.Add(this.getInfoPage);
			this.tab.Controls.Add(this.authenticatorTestPage);
			this.tab.Controls.Add(this.cryptPage);
			this.tab.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tab.Location = new System.Drawing.Point(0, 0);
			this.tab.Name = "tab";
			this.tab.SelectedIndex = 0;
			this.tab.Size = new System.Drawing.Size(392, 273);
			this.tab.TabIndex = 0;
			// 
			// getInfoPage
			// 
			this.getInfoPage.Controls.Add(this.info);
			this.getInfoPage.Controls.Add(this.getInfo);
			this.getInfoPage.Controls.Add(this.login);
			this.getInfoPage.Controls.Add(this.loginLabel);
			this.getInfoPage.Location = new System.Drawing.Point(4, 22);
			this.getInfoPage.Name = "getInfoPage";
			this.getInfoPage.Size = new System.Drawing.Size(384, 247);
			this.getInfoPage.TabIndex = 1;
			this.getInfoPage.Text = "Get Info";
			// 
			// info
			// 
			this.info.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																				   this.nameColumn,
																				   this.valueColumn});
			this.info.FullRowSelect = true;
			this.info.GridLines = true;
			this.info.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.info.Location = new System.Drawing.Point(8, 70);
			this.info.Name = "info";
			this.info.Size = new System.Drawing.Size(372, 172);
			this.info.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.info.TabIndex = 3;
			this.info.View = System.Windows.Forms.View.Details;
			// 
			// nameColumn
			// 
			this.nameColumn.Text = "Name";
			this.nameColumn.Width = 125;
			// 
			// valueColumn
			// 
			this.valueColumn.Text = "Value";
			this.valueColumn.Width = 215;
			// 
			// getInfo
			// 
			this.getInfo.Location = new System.Drawing.Point(128, 40);
			this.getInfo.Name = "getInfo";
			this.getInfo.TabIndex = 2;
			this.getInfo.Text = "Get Info...";
			this.getInfo.Click += new System.EventHandler(this.OnGetInfo);
			// 
			// login
			// 
			this.login.Location = new System.Drawing.Point(8, 40);
			this.login.Name = "login";
			this.login.TabIndex = 1;
			this.login.Text = "";
			// 
			// loginLabel
			// 
			this.loginLabel.Location = new System.Drawing.Point(8, 16);
			this.loginLabel.Name = "loginLabel";
			this.loginLabel.Size = new System.Drawing.Size(352, 23);
			this.loginLabel.TabIndex = 0;
			this.loginLabel.Text = "Enter the login name to get information from:";
			// 
			// authenticatorTestPage
			// 
			this.authenticatorTestPage.Controls.Add(this.logonProviderLabel);
			this.authenticatorTestPage.Controls.Add(this.logonProvider);
			this.authenticatorTestPage.Controls.Add(this.loginTypeLabel);
			this.authenticatorTestPage.Controls.Add(this.loginType);
			this.authenticatorTestPage.Controls.Add(this.status);
			this.authenticatorTestPage.Controls.Add(this.authenticate);
			this.authenticatorTestPage.Controls.Add(this.userName);
			this.authenticatorTestPage.Controls.Add(this.passwordLabel);
			this.authenticatorTestPage.Controls.Add(this.userNameLabel);
			this.authenticatorTestPage.Controls.Add(this.authenticationLabel);
			this.authenticatorTestPage.Controls.Add(this.password);
			this.authenticatorTestPage.Controls.Add(this.impersonate);
			this.authenticatorTestPage.Location = new System.Drawing.Point(4, 22);
			this.authenticatorTestPage.Name = "authenticatorTestPage";
			this.authenticatorTestPage.Size = new System.Drawing.Size(384, 247);
			this.authenticatorTestPage.TabIndex = 0;
			this.authenticatorTestPage.Text = "Authenticator Test";
			this.authenticatorTestPage.ToolTipText = "Test the Authenticator class";
			// 
			// loginTypeLabel
			// 
			this.loginTypeLabel.Location = new System.Drawing.Point(183, 100);
			this.loginTypeLabel.Name = "loginTypeLabel";
			this.loginTypeLabel.Size = new System.Drawing.Size(63, 23);
			this.loginTypeLabel.TabIndex = 7;
			this.loginTypeLabel.Text = "Login type:";
			this.loginTypeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// loginType
			// 
			this.loginType.Location = new System.Drawing.Point(252, 101);
			this.loginType.Name = "loginType";
			this.loginType.Size = new System.Drawing.Size(121, 21);
			this.loginType.TabIndex = 6;
			// 
			// status
			// 
			this.status.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.status.Location = new System.Drawing.Point(11, 163);
			this.status.Multiline = true;
			this.status.Name = "status";
			this.status.ReadOnly = true;
			this.status.Size = new System.Drawing.Size(363, 73);
			this.status.TabIndex = 5;
			this.status.Text = "";
			// 
			// authenticate
			// 
			this.authenticate.Location = new System.Drawing.Point(257, 42);
			this.authenticate.Name = "authenticate";
			this.authenticate.Size = new System.Drawing.Size(89, 23);
			this.authenticate.TabIndex = 3;
			this.authenticate.Text = "Authenticate";
			this.authenticate.Click += new System.EventHandler(this.OnAuthenticate);
			// 
			// userName
			// 
			this.userName.Location = new System.Drawing.Point(87, 42);
			this.userName.Name = "userName";
			this.userName.Size = new System.Drawing.Size(150, 20);
			this.userName.TabIndex = 0;
			this.userName.Text = "";
			// 
			// passwordLabel
			// 
			this.passwordLabel.Location = new System.Drawing.Point(8, 68);
			this.passwordLabel.Name = "passwordLabel";
			this.passwordLabel.Size = new System.Drawing.Size(73, 23);
			this.passwordLabel.TabIndex = 2;
			this.passwordLabel.Text = "Password:";
			this.passwordLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// userNameLabel
			// 
			this.userNameLabel.Location = new System.Drawing.Point(8, 41);
			this.userNameLabel.Name = "userNameLabel";
			this.userNameLabel.Size = new System.Drawing.Size(73, 23);
			this.userNameLabel.TabIndex = 1;
			this.userNameLabel.Text = "User Name:";
			this.userNameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// authenticationLabel
			// 
			this.authenticationLabel.Location = new System.Drawing.Point(8, 10);
			this.authenticationLabel.Name = "authenticationLabel";
			this.authenticationLabel.Size = new System.Drawing.Size(342, 23);
			this.authenticationLabel.TabIndex = 0;
			this.authenticationLabel.Text = "Enter the UserName and Password for authentication:";
			// 
			// password
			// 
			this.password.Location = new System.Drawing.Point(87, 69);
			this.password.Name = "password";
			this.password.PasswordChar = '*';
			this.password.Size = new System.Drawing.Size(150, 20);
			this.password.TabIndex = 1;
			this.password.Text = "";
			// 
			// impersonate
			// 
			this.impersonate.Location = new System.Drawing.Point(87, 100);
			this.impersonate.Name = "impersonate";
			this.impersonate.Size = new System.Drawing.Size(89, 23);
			this.impersonate.TabIndex = 4;
			this.impersonate.Text = "Impersonate";
			this.impersonate.Click += new System.EventHandler(this.OnImpersonate);
			// 
			// cryptPage
			// 
			this.cryptPage.Controls.Add(this.decrypt);
			this.cryptPage.Controls.Add(this.decryptedTextLabel);
			this.cryptPage.Controls.Add(this.encrypt);
			this.cryptPage.Controls.Add(this.plainText);
			this.cryptPage.Controls.Add(this.plainTextLabel);
			this.cryptPage.Controls.Add(this.encryptedText);
			this.cryptPage.Location = new System.Drawing.Point(4, 22);
			this.cryptPage.Name = "cryptPage";
			this.cryptPage.Size = new System.Drawing.Size(384, 247);
			this.cryptPage.TabIndex = 2;
			this.cryptPage.Text = "Crypt Test";
			// 
			// decrypt
			// 
			this.decrypt.Location = new System.Drawing.Point(283, 156);
			this.decrypt.Name = "decrypt";
			this.decrypt.TabIndex = 4;
			this.decrypt.Text = "Decrypt";
			this.decrypt.Click += new System.EventHandler(this.OnDecrypt);
			// 
			// decryptedTextLabel
			// 
			this.decryptedTextLabel.Location = new System.Drawing.Point(8, 84);
			this.decryptedTextLabel.Name = "decryptedTextLabel";
			this.decryptedTextLabel.Size = new System.Drawing.Size(83, 18);
			this.decryptedTextLabel.TabIndex = 3;
			this.decryptedTextLabel.Text = "Encrypted Text:";
			// 
			// encrypt
			// 
			this.encrypt.Location = new System.Drawing.Point(285, 44);
			this.encrypt.Name = "encrypt";
			this.encrypt.TabIndex = 2;
			this.encrypt.Text = "Encrypt";
			this.encrypt.Click += new System.EventHandler(this.OnEcrypt);
			// 
			// plainText
			// 
			this.plainText.Location = new System.Drawing.Point(98, 11);
			this.plainText.Name = "plainText";
			this.plainText.Size = new System.Drawing.Size(263, 20);
			this.plainText.TabIndex = 1;
			this.plainText.Text = "";
			// 
			// plainTextLabel
			// 
			this.plainTextLabel.Location = new System.Drawing.Point(25, 13);
			this.plainTextLabel.Name = "plainTextLabel";
			this.plainTextLabel.Size = new System.Drawing.Size(66, 16);
			this.plainTextLabel.TabIndex = 0;
			this.plainTextLabel.Text = "Plain Text:";
			this.plainTextLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// encryptedText
			// 
			this.encryptedText.Location = new System.Drawing.Point(98, 82);
			this.encryptedText.Multiline = true;
			this.encryptedText.Name = "encryptedText";
			this.encryptedText.Size = new System.Drawing.Size(260, 60);
			this.encryptedText.TabIndex = 3;
			this.encryptedText.Text = "";
			// 
			// errorProvider
			// 
			this.errorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
			this.errorProvider.ContainerControl = this;
			// 
			// logonProviderLabel
			// 
			this.logonProviderLabel.Location = new System.Drawing.Point(160, 127);
			this.logonProviderLabel.Name = "logonProviderLabel";
			this.logonProviderLabel.Size = new System.Drawing.Size(86, 23);
			this.logonProviderLabel.TabIndex = 9;
			this.logonProviderLabel.Text = "Logon Provider:";
			this.logonProviderLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// logonProvider
			// 
			this.logonProvider.Location = new System.Drawing.Point(252, 128);
			this.logonProvider.Name = "logonProvider";
			this.logonProvider.Size = new System.Drawing.Size(121, 21);
			this.logonProvider.TabIndex = 8;
			// 
			// SecurityTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(392, 273);
			this.Controls.Add(this.tab);
			this.Name = "SecurityTest";
			this.Text = "SecurityTest";
			this.tab.ResumeLayout(false);
			this.getInfoPage.ResumeLayout(false);
			this.authenticatorTestPage.ResumeLayout(false);
			this.cryptPage.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region Entry Point
		[STAThread]
		static void Main() 
		{
			Application.Run(new SecurityTest());
		}
		#endregion
	}
}

// --- EOF ---
